﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Job.Data.Interfaces;
using Job.Logic.Common;
using Job.Entity;
using Job.Web.Framework;

namespace Job.Logic.BaseContent
{
    public class HunterJobLogic
    {
        private readonly IJobHunterDao dao = DataAccess.jobHunterDao;
        private readonly ICommon commonDao = DataAccess.commonDao;

        private const string BACK_LIST_SQL = @"Select job_id,job_no,name,industry,salary,office_address,create_date From job_hunter";

        //不能存在相同JobNo的记录
        public bool Insert(JobHunter bean)
        {
            IList<JobHunter> list = dao.FindByJobNo(bean.JobNo);
            if (list == null || list.Count == 0)
            {
                dao.Insert(bean);
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool Update(JobHunter bean)
        {
            dao.Update(bean);
            return true;
        }

        public void DeleteById(int jobId)
        {
            JobHunter bean = new JobHunter();
            bean.JobId = jobId;
            dao.Delete(bean);
        }
        public void DeleteByNo(string jobNo)
        {
            dao.DeleteByJobNo(jobNo);
        }


        public JobHunter GetDetail(int jobId)
        {
            return dao.Find(jobId);
        }
        public JobHunter GetDetail(String jobNo)
        {
            IList<JobHunter> list = dao.FindByJobNo(jobNo);
            if (null == list || list.Count == 0) return null;
            return list[0];
        }

        public int GetCount() 
        {
            CommonLogic common = new CommonLogic();
            int count = common.GetCountBySql(BACK_LIST_SQL);
            return count;
        }

        private string GetWhere(string key)
        {
            key = (null == key) ? "" : key.Trim();
            if (key.Equals("")) return " 1=1 ";

            key = key.Replace("'","''");
            StringBuilder buf = new StringBuilder();
            buf.Append("      name like '%");
            buf.Append(key);
            buf.Append("%' Or industry like '%");
            buf.Append(key);
            buf.Append("%' Or office_address like '%");
            buf.Append(key);
            buf.Append("%' Or responsibility like '%");
            buf.Append(key);
            buf.Append("%' Or grade like '%");
            buf.Append(key);
            buf.Append("%'");
            return buf.ToString();
        }

        public int GetCount(string key)
        {
            CommonLogic common = new CommonLogic();
            string sql = "Select * From job_hunter Where " + GetWhere(key);
            int count = common.GetCountBySql(sql);
            return count;
        }

        public DataTable GetJobPageList(int pageSize, int pageIndex)
        {
            CommonLogic common = new CommonLogic();
            DataSet ds = common.GetPageDataBySql(BACK_LIST_SQL, pageSize, pageIndex, 0,
                "create_date",SortDirection.Descending,null);
            return ds.Tables[0];
        }
        public DataTable GetJobPageList(int pageSize, int pageIndex,string key)
        {
            CommonLogic common = new CommonLogic();
            string sql = BACK_LIST_SQL + " Where " + GetWhere(key);
            DataSet ds = common.GetPageDataBySql(sql, pageSize, pageIndex, 0,
                "create_date", SortDirection.Descending, null);
            return ds.Tables[0];
        }


        public DataTable GetTopJob(int number)
        {
            return GetJobPageList(number, 1);
        }

    }
}
